
package BattleLevel

import(
	"fmt"
	"sanguo/node/table/excel"
	"sanguo/node/table/excel/table"
	"strconv"

	"github.com/tealeg/xlsx"
)

func readInt32(v string) int32 {
	return int32(readFloat(v))
}

func readFloat(v string) float64 {
	vv,err := strconv.ParseFloat(v,64)
	if nil != err {
		panic(err)
	}
	return vv
}

func readBool(str string) bool {
    b, err := strconv.ParseBool(str)
	if nil != err {
		panic(err)
	}
    return b
}

func readStr(v string) string {
	return fmt.Sprintf("%s",v)
}

type Position_ struct {
	X int32
	Y int32
	Z int32
}

type ItemCost_ struct {
	ItemID int32
	ItemCount int32
}

type BattleLevel struct {
	ID int32
	Type int32
	IsReconnet bool
	MapID int32
	Position Position_
	Angle int32
	NeedTP bool
	PreconditionLevel int32
	IsReturnWhenFail bool
	IsResurgence bool
	ResurgenceId int32
	ActionPointCost int32
	GoldCost int32
	DiamondCost int32
	ItemCost []ItemCost_
	StageConfig string
	DropPool int32
}

func readPosition(value string) Position_ {

	Position := Position_{}
	r := excel.Split(value,",")

	Position.X = readInt32(r[0][0])

	Position.Y = readInt32(r[0][1])

	Position.Z = readInt32(r[0][2])
		
	return Position
}

func readAddItemCost(names, row *xlsx.Row) []ItemCost_ {
			
	ItemCost := make([]ItemCost_,0)
	r := excel.RowSplit("CostItem_,CostItemCount_;1,3")
	_, err := strconv.ParseFloat(r[0][0], 64)
	
	if err == nil {		
		for _,v := range(r) {
			ItemCost_ := ItemCost_{}

			ItemCost_.ItemID = readInt32(row.Cells[readInt32(v[0])].String())
				
			ItemCost_.ItemCount = readInt32(row.Cells[readInt32(v[1])].String())
				
			ItemCost = append(ItemCost,ItemCost_)
		}
	} else {
		for i := readInt32(r[1][0]); i <= readInt32(r[1][1]); i++ {
			ItemCost_ := ItemCost_{}

			ItemCost_.ItemID = readInt32(row.Cells[getIdxByName(names, fmt.Sprintf("%s%d", r[0][0], i))].String())
				
			ItemCost_.ItemCount = readInt32(row.Cells[getIdxByName(names, fmt.Sprintf("%s%d", r[0][1], i))].String())
				
			ItemCost = append(ItemCost,ItemCost_)
		}
	}
	return ItemCost
}

type Table struct {
	IDMap map[int32]*BattleLevel
	tmpIDMap map[int32]*BattleLevel
	xlsxName string
}

var Table_ Table

func getIdxByName(names *xlsx.Row, name string) int {
	for i, v := range names.Cells {
		if v.String() == name {
			return i
		}
	}
	return -1
}

func init() {
	Table_.xlsxName = "BattleLevel.xlsx"
	Table_.IDMap = make(map[int32]*BattleLevel)
	Table_.tmpIDMap = make(map[int32]*BattleLevel)

	table.AddTable(&Table_)
}


func (this *Table) Load(path string) {

	excel.EmptyFunc()

    xlFile, err := xlsx.OpenFile(path + this.xlsxName)
    if err != nil {
		panic(err)
    }

    sheet := xlFile.Sheets[0]
    names := sheet.Rows[1]
    rows  := sheet.Rows[3:]

    for _,row := range(rows) {
    	if len(row.Cells) > 0 && row.Cells[0].String() != "" {
    		ele := &BattleLevel{}
    	
		ele.ID = readInt32(row.Cells[getIdxByName(names, "ID")].String())
		this.tmpIDMap[ele.ID] = ele
		ele.Type = readInt32(row.Cells[getIdxByName(names, "Type")].String())
		ele.IsReconnet = readBool(row.Cells[getIdxByName(names, "IsReconnet")].String())
		ele.MapID = readInt32(row.Cells[getIdxByName(names, "MapID")].String())
		ele.Position = readPosition(row.Cells[getIdxByName(names, "Position")].String())
		ele.Angle = readInt32(row.Cells[getIdxByName(names, "Angle")].String())
		ele.NeedTP = readBool(row.Cells[getIdxByName(names, "NeedTP")].String())
		ele.PreconditionLevel = readInt32(row.Cells[getIdxByName(names, "PreconditionLevel")].String())
		ele.IsReturnWhenFail = readBool(row.Cells[getIdxByName(names, "IsReturnWhenFail")].String())
		ele.IsResurgence = readBool(row.Cells[getIdxByName(names, "IsResurgence")].String())
		ele.ResurgenceId = readInt32(row.Cells[getIdxByName(names, "ResurgenceId")].String())
		ele.ActionPointCost = readInt32(row.Cells[getIdxByName(names, "ActionPointCost")].String())
		ele.GoldCost = readInt32(row.Cells[getIdxByName(names, "GoldCost")].String())
		ele.DiamondCost = readInt32(row.Cells[getIdxByName(names, "DiamondCost")].String())
		ele.ItemCost = readAddItemCost(names, row)
		ele.StageConfig = readStr(row.Cells[getIdxByName(names, "StageConfig")].String())
		ele.DropPool = readInt32(row.Cells[getIdxByName(names, "DropPool")].String())

		}
    }

    this.IDMap = this.tmpIDMap
 
}

